<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="单位名称" prop="companyName">
      <el-input v-model="dataForm.companyName" placeholder="实习单位名称"></el-input>
    </el-form-item>
    <el-form-item label="负责人" prop="companyPerson">
      <el-input v-model="dataForm.companyPerson" placeholder="负责人"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮件信息"></el-input>
    </el-form-item>
    <el-form-item label="单位地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="单位详细地址"></el-input>
    </el-form-item>
    <el-form-item label="单位类型" prop="type">
      <el-select v-model="dataForm.type" placeholder="请选择">
        <el-option
          v-for="item in typeOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="企业类型" prop="industryType">
      <el-input v-model="dataForm.industryType" placeholder="企业类型"></el-input>
    </el-form-item>
    <el-form-item label="职位名称" prop="position">
      <el-input v-model="dataForm.position" placeholder="职位名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        typeOptions: [
          {
            id: 0,
            name: '国企'
          },
          {
            id: 1,
            name: '民企'
          }
        ],
        dataForm: {
          id: 0,
          stuId: '',
          companyName: '',
          companyPerson: '',
          phone: '',
          email: '',
          address: '',
          type: '',
          industryType: '',
          position: ''
        },
        dataRule: {
          stuId: [
            { required: true, message: '学生ID不能为空', trigger: 'blur' }
          ],
          companyName: [
            { required: true, message: '实习单位名称不能为空', trigger: 'blur' }
          ],
          companyPerson: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件信息不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '单位详细地址不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '单位类型 0：国企，1：民企不能为空', trigger: 'blur' }
          ],
          industryType: [
            { required: true, message: '企业类型不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '职位名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/stuinterncompany/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.stuId = data.stuInternCompany.stuId
                this.dataForm.companyName = data.stuInternCompany.companyName
                this.dataForm.companyPerson = data.stuInternCompany.companyPerson
                this.dataForm.phone = data.stuInternCompany.phone
                this.dataForm.email = data.stuInternCompany.email
                this.dataForm.address = data.stuInternCompany.address
                this.dataForm.type = data.stuInternCompany.type
                this.dataForm.industryType = data.stuInternCompany.industryType
                this.dataForm.position = data.stuInternCompany.position
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/stuinterncompany/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'stuId': this.dataForm.stuId,
                'companyName': this.dataForm.companyName,
                'companyPerson': this.dataForm.companyPerson,
                'phone': this.dataForm.phone,
                'email': this.dataForm.email,
                'address': this.dataForm.address,
                'type': this.dataForm.type,
                'industryType': this.dataForm.industryType,
                'position': this.dataForm.position
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
